package com.example.cslp.service.Impl;


import com.example.cslp.mapper.UserMapper;
import com.example.cslp.pojo.entity.Course;
import com.example.cslp.pojo.entity.User;
import com.example.cslp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    public UserMapper userMapper;
    //登录
    public ResponseEntity<Boolean> login(User user){
        User user1=userMapper.login(user.getName());
        //判断是什么端登录
        if(user1.getPort().equals(user1.getIsAdmin())){
            if(user1!=null&&user1.getName()!=null){
                if(user.getPassword().equals(user1.getPassword())){
                    System.out.println("1");
                    return ResponseEntity.ok(true);
                }
                else {
                    System.out.println("2");
                    return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(false);
                }
            }
            else {
                System.out.println("3");
                return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(false);
            }
        }
        else{
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(false);
        }

    }
    //注册注册注册注册注册注册注册注册注册注册注册注册注册
    public ResponseEntity<Boolean> register(User user){
        System.out.println("1");
        if(user.getName()==null||user.getPassword()==null){
            //返回false
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(false);
        }
        else{
            if(userMapper.login(user.getName())!=null){
                //返回假
                return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(false);
            }
            else {
                //返回真
                return ResponseEntity.ok(userMapper.register(user));
            }
        }
    }
    //查询用户信息
    public User getUserInfo(String name){
        return userMapper.getUserInfo(name);
    }
    //签到加积分
    public Boolean point(User user){
        return userMapper.point(user);
    }
    //编辑用户信息
//    public Boolean editUser(User user){
//        return userMapper.editUser(user);
//    }
    @Override
    public Boolean editInfo(User user,String check_name){
        return userMapper.editInfo(user,check_name);
    }
    //编辑账户使用状态
    public Boolean editStatus(User user){
        return userMapper.editStatus(user);
    }

    //    //根据用户获取选课信息
//    public ResponseEntity<List<Course>> getRecordByUser(User user){
//        return ResponseEntity.ok(userMapper.getRecordByUser(user));
//    }
    //修改账户密码
//    public Boolean editPassword(User user){
//        return userMapper.editPassword(user);
//    }
    //编辑用户账密
    @Override
    public Boolean editPassword(String new_password, String id) {
        return userMapper.editPassword(new_password, id);
    }

    //根据id查询用户信息
    public User getUserInfoById(String id){
        return userMapper.getUserInfoById(id);
    }

}